In [ ]:
import networkx as nx
NetworkX has many built in functions to read data from a variety of formats. Because formats can be pretty esoteric this is also the source of many bugs. Please if you find them report them. Here is a brief list of the file formats NetworkX can read and write, and their corresponding function names
Format | Read Function | Write Function |
---|---|---|
Adjacency List | nx.read_adjlist |
nx.write_adjlist |
Edge List | nx.read_edgelist |
nx.write_edgelist |
Graph Exchange XML Format (GEXF) | nx.read_gexf |
nx.write_gexf |
Graph Modeling Language (GML) | nx.read_gml |
nx.write_gml |
Pickle, Python's serialization format for NetworkX graphs | nx.read_gpickle |
nx.write_gpickle |
GraphML | nx.read_graphml |
nx.write_graphml |
Leda | nx.read_leda |
nx.write_leda |
Multiline Adjacency List | nx.read_multiline_adjlist |
nx.write_multiline_adjlist |
Esri Shapefile | nx.read_shp |
nx.write_shp |
Yet Another Markup Language (YAML) | nx.read_yaml |
nx.write_yaml |
p2g (metabolic pathways) | nx.read_p2g |
nx.write_p2g |
pajek | nx.read_pajek |
nx.write_pajek |
sparse6 | nx.read_sparse6 |
nx.write_sparse6 |
./data/
folder
In [ ]:
It is often useful to create graphs from other types of python data structures. In particular, graphs are often represented by matrices. If your data has a matrix represenatation, NetworkX can easily create a graph using from_numpy_matrix
In [ ]:
import numpy as np
In [ ]:
n = 25
A = np.random.binomial(1,1.1/n,size=(n,n)) # Random 1/s with probability 1/25
G = nx.from_numpy_matrix(A)
In [ ]:
G.order()
In [ ]:
G.size()
In [ ]:
G.degree()
What kind of graph is the one above?
NetworkX can handle other data structures such as a list of edges (from_edgelist
) and scipy
sparse matrices (scipy_sparse_matrix
). You can use the create_using
keyword to make a DiGraph
s or MultiGraph
s.
In [ ]:
edges = []
for u in range(n):
for v in range(n):
if u % 3==0 and u>v:
edges.append((u,v))
elif u % 3 == 1 and v < u:
edges.append((u,v))
In [ ]:
D = nx.from_edgelist(edges,create_using=nx.DiGraph())
You can also create matrices out of already created graphs
In [ ]:
G = nx.Graph()
for u in range(5):
G.add_edge(u,u,index=u) # Self loops!
In [ ]:
nx.to_numpy_matrix(G)
You can output weighted matrices too
In [ ]:
nx.to_numpy_matrix(G,weight='index')
Another obvious way to create graphs is to use other graphs. This can be especially useful when coverting between Graph
s and DiGraphs
or making copies of graphs for modification.
In [ ]:
D = nx.DiGraph()
D.add_star(range(5))
D.add_cycle(range(5,10))
D.add_edge(4,5)
In [ ]:
G = nx.Graph(D)
In [ ]:
G.edges()
In [ ]:
G = nx.Graph()
G.add_star(range(5))
D = nx.DiGraph(G)
In [ ]:
D.edges()
Notice when we create a directed graph from a graph we get edges in both directions.
Finally, NetworkX includes a number of graph operations to make combining graphs easier. First some exercises so we have some graphs to mess with.
C
of five nodesL
of 10 nodesS
with of 7 nodes with 0 at it's center
In [ ]:
In [ ]:
In [ ]:
Try out these functions, what do they produce?
nx.compose(C,L)
nx.cartesian_product(S,L)
nx.complement(S)
To get ready for the next section let's write a function that implements the Kronecker Graph of order k. This is simply the tensor_product
of a graph applied to itself k times.
That is, if we have a graph $G$, with adjacency matrix $A$ and kronecker product(tensor product) $\otimes$, the Kronecker Graph of order $k$ is a graph with Adjacency matrix
Let's also assume (as they do in the paper) that the kronecker graph always has self loops on all the nodes.
I'll give you the function stub and you can fill in the details
In [ ]:
def kronecker_graph(G,k):
K = nx.Graph(G) # For the kronecker graph we are going to return
B = nx.Graph(G) # For the base graph
for n in K:
#Add self loops to both K and B
for i in range():# Add the number of kronecker products here
#Create K using the appropriate networkx product function
return K
Make a kronecker graph of order $k=2$ out of the Line graph L
.
In [ ]:
K = kronecker_graph(L,2)